package 题目集.二分;

/**
 * 原题：https://codeforces.com/problemset/problem/773/A
 * 坐牢题
 * 给你一个现有值a，b
 * 给你一个期望值c，d
 * 问让a/b==c/d相同至少需要多少次
 * 暴力思路：
 *      枚举c的倍数，令a'=x*c
 *      如果a'/b < c/d，则继续令a'=x*c（x递增）
 *      如果a'/b > c/b，则b+=n-(b-d)%d
 */

import java.math.BigInteger;
import java.util.Scanner;

public class ch10_成功率 {
    static long a, b, c, d;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            a = sc.nextLong();
            b = sc.nextLong();
            c = sc.nextLong();
            d = sc.nextLong();
            if (b==0){
                if (c==0){
                    System.out.println(0);
                    continue;
                }
                if (c==d){
                    System.out.println(1);
                    continue;
                }
            }
            if ((c == d && a < b) || (c == 0 && a != 0) || (d == 0 && b != 0)) {
                System.out.println(-1);
            } else {
                if (a * d == b * c) {
                    System.out.println(0);
                    continue;
                }
                long l = find();
                System.out.println(l);
            }
        }
    }

    public static long find() {
        long l = (long) Math.ceil(1.0 * a / c), r = (long) 1e9;  //r的范围有待推敲
        while (l <= r) {
            long m = l + (r - l >> 1);
            if (check(c * m)) {    //m大了
                r = m - 1;
            } else {
                l = m + 1;
            }
        }
        long difA = c * l - a;
        long difB = c * l * d / c - (b + difA);
        if (check(c * r)) {
            difA = c * r - a;
            difB = c * l * d / c - (b + difA);
        }
        return difA + difB;
    }

    public static boolean check(long m) {
        return m * d >= (b + m - a) * c;
    }
}
